﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;

namespace GgCarControl
{
    public partial class AutoSendCarSetForm : DockContent
    {
        public static Database db =new OracleDatabase("Data Source=DG;User ID=dgrfid;Password=dgrfid;Unicode=True;Load Balance Timeout=1000");

        public AutoSendCarSetForm()
        {
            InitializeComponent();
        }

        private void AutoSendCarForm_Load(object sender, EventArgs e)
        {
            ComboBoxBind(cbStackName, "STACKNAME");
            ComboBoxBind(cbGetOwner, "GETOWNER");
            ComboBoxBind(cbYard, "YARD");
            ComboBoxBind(cbZYJX, "ZYJX");
            LoadData();
        }

        private void ComboBoxBind(ComboBox ctl, string filedName)
        {
            ctl.Items.Clear();
            string strsq = " SELECT DISTINCT " + filedName + " FROM TRUCK_CARGO_LOGIN order by " + filedName;
            DataTable dt = db.ExecuteDataSet(db.GetSqlStringCommand(strsq.ToString())).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (!dr.IsNull(0))
                        ctl.Items.Add(dr[0].ToString());
                }
            }
        }

        private void QueryData()
        {
            string strSQL = "select * from TRUCK_AUTO_SENDCAR where 1=1";
            string strStackName = this.cbStackName.Text.Trim();
            string strGetOwner = this.cbGetOwner.Text.Trim();
            string strYard = this.cbYard.Text.Trim();
            string strZYJX = this.cbZYJX.Text.Trim();
            if (!string.IsNullOrEmpty(strStackName))
                strSQL += " and stackname like '%" + strStackName + "%'";
            if (!string.IsNullOrEmpty(strGetOwner))
                strSQL += " and getowner like '%" + strGetOwner + "%'";
            if (!string.IsNullOrEmpty(strYard))
                strSQL += " and yard like '%" + strYard + "%'";
            if (!string.IsNullOrEmpty(strZYJX))
                strSQL += " and zyjx like '%" + strZYJX + "%'";

            this.dgvAutoSendCar.AutoGenerateColumns = false;
            this.dgvAutoSendCar.DataSource = db.ExecuteDataSet(db.GetSqlStringCommand(strSQL)).Tables[0];
        }

        private void LoadData()
        {
            string strSQL = "select * from TRUCK_AUTO_SENDCAR";

            this.dgvAutoSendCar.AutoGenerateColumns = false;
            this.dgvAutoSendCar.DataSource = db.ExecuteDataSet(db.GetSqlStringCommand(strSQL)).Tables[0];
        }

        private string create_id()
        {
            string sequence = "";
            string sqlStr = "select SEQ_TRUCK.nextval from dual";
            DataSet ds1 = db.ExecuteDataSet(db.GetSqlStringCommand(sqlStr));
            if (ds1.Tables[0].Rows.Count > 0)
            {
                sequence = ds1.Tables[0].Rows[0][0].ToString();
            }
            string year_str = DateTime.Now.Year.ToString();
            string month_str = DateTime.Now.Month.ToString();
            string day_str = DateTime.Now.Day.ToString();
            string id = year_str + month_str + day_str + sequence;
            return id;
        }

        private void btnAddStack_Click(object sender, EventArgs e)
        {
            string strStackName = this.cbStackName.Text.Trim();
            string strGetOwner = this.cbGetOwner.Text.Trim();
            string strYard = this.cbYard.Text.Trim();
            string strZYJX = this.cbZYJX.Text.Trim();
            if (string.IsNullOrEmpty(strStackName))
            {
                MessageBox.Show("垛名不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(strGetOwner))
            {
                MessageBox.Show("收货人不能为空");
                return;
            }
            if (string.IsNullOrEmpty(strYard))
            {
                MessageBox.Show("作业地点不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(strZYJX))
            {
                MessageBox.Show("作业机械不能为空");
                return;
            }

            string sID = create_id();
            string strSQL = "insert into TRUCK_AUTO_SENDCAR(ID,STACKNAME,GETOWNER,ADDMAN,ADDTIME,YARD,ZYJX) values(" +
                sID + ",'" + strStackName + "','" + strGetOwner + "','" + Session.XM + "',sysdate,'" + strYard + "','" + strZYJX + "')";
            int i = db.ExecuteNonQuery(db.GetSqlStringCommand(strSQL));
            if (i > 0)
            {
                if (MessageBox.Show("添加成功！是否自动放行已登记的符合条件车辆？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    AutoSendCar(strStackName, strGetOwner, strYard, strZYJX);
                }
                LoadData();
            }
        }

        private void AutoSendCar(string strStackName, string strGetOwner, string strYard, string strZYJX)
        {
            string strSQL = @"update TRUCK_CARGO_LOGIN set TRUCKSTATUS=1,COMMANDTIME=sysdate,ISAUTO='1',YARD='{2}',ZYJX='{3}' 
                              where truckstatus='0' and coalesce(cancel_flag,'0')<>'1' and stackname ='{0}' and getowner='{1}'";
            strSQL = string.Format(strSQL, strStackName, strGetOwner, strYard, strZYJX);
            db.ExecuteNonQuery(db.GetSqlStringCommand(strSQL));
            //记录日志
            //string new_id = create_id();
            //string op_user = Session.Operator_User.ToString();
            //string insert_sql = "insert into TRUCK_LOG(ID,TRUCKNO,OPERATENAME,XGSJ,XGNR) values(" + new_id +
            //    ",'" + strTruckNo + "','" + op_user + "',sysdate,'停车场自动放车确认')";
            //int ret2 = ws.execute_UID_Serv(insert_sql);
            //if (ret2 < 0) return;           

            MessageBox.Show("处理成功！");
        }

        private void btnDeleteStack_Click(object sender, EventArgs e)
        {
            if (this.dgvAutoSendCar.SelectedRows != null && this.dgvAutoSendCar.SelectedRows.Count > 0)
            {
                string strSQL = "delete from TRUCK_AUTO_SENDCAR where ID = {0}";
                try
                {
                    foreach (DataGridViewRow row in this.dgvAutoSendCar.SelectedRows)
                    {
                        string strID = row.Cells["ID"].Value.ToString();
                        db.ExecuteNonQuery(db.GetSqlStringCommand(string.Format(strSQL, strID)));
                    }
                    MessageBox.Show("删除成功！");
                    LoadData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除失败！ " + ex.Message);
                }
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }
    }
}
